import { useState, useEffect } from 'react'
import { useStore } from 'react-redux'
import http from '../../../api'


//自定义hook用法类似vue的mixin 需要使用use开头
export default function usePubTableData(type) {
    const [pubTableData, setPubTableData] = useState([])
    const { UserInfo } = useStore().getState()

    useEffect(() => {
        getTableList()
    }, [])



    const getTableList = async () => {
        const classRes = await http.get('/categories')
        const newRes = await http.get(`/news?author=${UserInfo.username}`)
        //需要获取新闻分类名称 并且过滤出审核和未发布的新闻
        const newList = newRes.data.map(item => {
            item.categoryName = classRes.data.find(classItem => classItem.id == item.categoryId).title
            return item
            // const publishList = ['未发布', '待发布', '已上线', '已下线']
        }).filter(item => item.publishState == type)
        setPubTableData(newList)
    }

    return { pubTableData, getTableList }
}